package com.lwb.ztx.videos.wedget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lwb.ztx.R;
import com.lwb.ztx.bean.VideoBean;
import com.lwb.ztx.utils.ImageLoaderUtils;
import com.lwb.ztx.utils.ListUtils;
import com.lwb.ztx.utils.LogUtils;
import com.lwb.ztx.utils.StringUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * Created by luweibin on 2016/5/30.
 */
public class VideoAdapter extends RecyclerView.Adapter {
    private List<VideoBean> beans;
    private Context context;

    public VideoAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LogUtils.i("create");
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.video_item_layout, parent, false);
        return new VideoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        VideoBean bean = beans.get(position);
        VideoViewHolder videoViewHolder = (VideoViewHolder) holder;
        videoViewHolder.videoPlayerStandard.release();
        videoViewHolder.videoPlayerStandard.setUp(bean.getVideoUrl(), bean.getTitle());
        videoViewHolder.videoPlayerStandard.titleTextView.setTextSize(14f);
        ImageLoaderUtils.display(context, videoViewHolder.videoPlayerStandard.thumbImageView, bean.getImage());
        videoViewHolder.comments.setText(bean.getCommentsall());
        videoViewHolder.playTimes.setText(bean.getPlayTime());
        videoViewHolder.length.setText("时长: " + StringUtils.getForMateSeconds(bean.getDuration()));
        videoViewHolder.videoPlayerStandard.thumbImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        videoViewHolder.videoPlayerStandard.thumbImageView.setBackgroundColor(0xFFFFFFFF);
    }

    @Override
    public int getItemCount() {
        return ListUtils.getSize(beans);
    }

    class VideoViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.video_view)
        JCVideoPlayerStandard videoPlayerStandard;
        @InjectView(R.id.video_play_times)
        TextView playTimes;
        @InjectView(R.id.video_comments)
        TextView comments;
        @InjectView(R.id.video_length)
        TextView length;

        public VideoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this,itemView);
        }
    }

    public void setBeans(List<VideoBean> beans) {
        this.beans = beans;
    }

    public void addMoreBeans(List<VideoBean> beans) {
        this.beans.addAll(beans);
    }
}
